<?php
declare(strict_types=1);

namespace App\Controller\Api\V1;

use App\Jobs\DelQiniuFile;
use App\Jobs\MineOrder;
use App\Model\LiveChargeOrder;
use App\Jobs\ScreenJob;
use App\Jobs\PlayerScreenRepush;
use App\Service\RockyPayLibrary2;
use App\Service\UserService;
use Hyperf\HttpServer\Contract\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\Di\Annotation\Inject;
use Hyperf\WebSocketServer\Sender;
use Hyperf\AsyncQueue\Driver\DriverInterface;
use Hyperf\AsyncQueue\Driver\DriverFactory;
use Hyperf\Utils\ApplicationContext;
use App\Service\Message\SendPlayerScreenMsgService;
use Hyperf\DbConnection\Db;

/**
 * class SystemController
 * @package App\Controller\Api\V1
 * @Controller(prefix="/api/v1/system")
 */
class SystemController extends CController
{
    /**
     * @var DriverInterface
     */
    protected $driver;
    /**
     * @Inject
     * @var Sender
     */
    public $sender;

    /**
     * 微信支付回调
     *
     * @RequestMapping(path="charge/{id}/wx-notify", methods="post")
     */
    public function chargeNotify()
    {

        $id = $this->request->route('id');

        $params = $this->request->all();
        info('wxchargenofity', $params);

        $rechargeOrder = LiveChargeOrder::where('id', $id)->where('status', 100)->first();
        if (empty($rechargeOrder)) {
            return 'SUCCESS';
        }
        $paymentMechod = data_get($rechargeOrder, 'payment_method', 'wx_mp');

        $tmp = explode('_', $paymentMechod);
        if (data_get($tmp, 0, 'wx') == 'wx') {
            $driver = 'wechat';
        } else {
            throw new \LogicException('error', 100);
        }
        $rockyPay = new RockyPayLibrary2([], $driver);
        $notifyInfo = $rockyPay->wxMpNotify(data_get($tmp, 1, 'mp'), $params);
        info('order微信支付 解析', [$notifyInfo]);


        if ($notifyInfo['result_code'] == 'SUCCESS' && $notifyInfo['return_code'] == 'SUCCESS') {
            $upd['amount'] = $notifyInfo['total_fee'] / 100;
            $data['status'] = 200;
            $data['payment_txn'] = $notifyInfo['transaction_id'];
            $rechargeOrder->update($data);
            UserService::commonTransaction(data_get($rechargeOrder, 'user_id', ''), data_get($rechargeOrder, 'points', ''), data_get($rechargeOrder, 'id', ''), 'live_charge_order', '充值'.data_get($rechargeOrder, 'product_name', ''));
        }

        return 'SUCCESS';
    }

    /**
     * 微信支付回调
     *
     * @RequestMapping(path="wsclose", methods="post")
     */
    public function close(RequestInterface $request)
    {
        $params = $request->all();
        $fd = data_get($params, 'client_id', '');
        try {
            $res = $this->sender->disconnect((int)$fd);
        } catch (\Exception $e) {
            throw new \LogicException('消息转发失败', 1005);
        }
        return $res;
    }


    /**
     * 出发大屏队列
     *
     * @RequestMapping(path="scd", methods="post")
     */
    public function screenJob(RequestInterface $request)
    {

        $params = $request->all();
        info('scd', $params);
        $playerId = data_get($params, 'player_id', '');
        $cpPackageId = data_get($params, 'cp_package_id', '');
        $key = redis()->get('SCREEN_SCD_XIANPIN_'.$cpPackageId.'_'.$playerId);
        if ($key) {
            return 1;
        }

        redis()->set('SCREEN_SCD_XIANPIN_'.$cpPackageId.'_'.$playerId, 1, 60);
        $time = data_get($params, 'time', '');


        $driver = ApplicationContext::getContainer()->get(DriverFactory::class)->get('screen');
        return $driver->push(new ScreenJob($params), 0);
    }

    /**
     * 触发补推
     *
     * @RequestMapping(path="pscd", methods="post")
     */
    public function sendPlayerScreen(RequestInterface $request){
        $params = $request->all();
        info('pscd', $params);

        $id = data_get($params, 'id');
        $screen_log = Db::table('screen_dashboard_logs')->where('id', '=', $id)->first();

        if(empty($screen_log)){
            return false;
        }

        $driver = ApplicationContext::getContainer()->get(DriverFactory::class)->get('screen');
        return $driver->push(new PlayerScreenRepush([
            'screen_id' => data_get($screen_log, 'screen_id'),
            'message' => json_decode(data_get($screen_log, 'message', []), true),
        ]), 0);
    }


    /**
     * 触发删七牛文件队列
     *
     * @RequestMapping(path="dqf", methods="post")
     */
    public function delQiniuFileJob(RequestInterface $request)
    {

        $params = $request->all();
        info('scd', $params);

        $driver = ApplicationContext::getContainer()->get(DriverFactory::class)->get('del_qiniu_file');
        return $driver->push(new DelQiniuFile($params), 0);
    }

    /**
     * 触发修改订单mine id队列
     *
     * @RequestMapping(path="uom", methods="post")
     */
    public function updateOrderMineId(RequestInterface $request)
    {

//        $params = $request->all();
//        info('uom', $params);

        return 'success';
//        $driver = ApplicationContext::getContainer()->get(DriverFactory::class)->get('update_order_mine');
//        return $driver->push(new MineOrder($params), 0);
    }

}
